# @Time    : 2021/10/5 13:44
# @Author  : mirrorlied
# @Comment : 模型相关工具函数

from sklearn.model_selection import train_test_split
import numpy as np
import random


# 模型性能测试
def PerformanceTesting(model, X, labels) -> float:
    """
    :param model: 使用模型
    :param X: X
    :param labels: labels
    :return: ACC
    """
    X_train, X_test, labels_train, labels_test = \
        train_test_split(X, labels, test_size=0.2, random_state=random.randint(1, 1000))
    # 训练
    model.fit(X_train, labels_train)
    # 预测
    pred = model.predict(X_test)
    # 归一化
    pred = np.where(pred > 0.5, 1, 0)
    return np.sum(np.array(pred) == np.array(np.array(labels_test))) / len(pred)


# 训练并预测
def TrainAndPredict(model, X_train, labels_train, X_test):
    """
    :param model: 模型
    :param X_train: 训练数据
    :param labels_train: 训练标签
    :param X_test: 预测数据
    :return: 预测标签
    """
    # 训练
    model.fit(X_train, labels_train)
    # 预测
    pred = model.predict(X_test)
    # 归一化
    pred = np.where(pred > 0.5, 1, 0)
    return pred








